package com.liuchuanzheng.lcz_compose_news

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.HorizontalPagerIndicator
import com.google.accompanist.pager.rememberPagerState
import com.liuchuanzheng.lcz_compose_news.ui.theme.LCZ_Compose_NewsTheme
import com.liuchuanzheng.lcz_compose_news.utils.SpUtils

/**
 * @author 刘传政
 * @date 2021/7/30 15:58
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
class GuideActivity : ComponentActivity() {
    companion object {
        const val HAS_SHOW_GUIDE = "has_show_guide"
    }

    @ExperimentalPagerApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (SpUtils.getBoolean(HAS_SHOW_GUIDE)) {
            go2Main()
        } else {
            setContent {
                LCZ_Compose_NewsTheme {
//                    Greeting(name = "刘传政")
                    GuidPage(images = listOf(R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3)) {
                        SpUtils.setBoolean(HAS_SHOW_GUIDE,true)
                        go2Main()
                    }
                }
            }
        }

    }

    private fun go2Main() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }
}

@ExperimentalPagerApi
@Composable
fun GuidPage(images: List<Int>, go2Main: () -> Unit) {
    Box(Modifier.fillMaxSize()) {
        val pagerState = rememberPagerState(pageCount = images.size, initialOffscreenLimit = 2)
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.fillMaxSize()
        ) { page: Int ->
            Image(
                painter = painterResource(id = images[page]),
                contentDescription = null,
                contentScale = ContentScale.FillBounds, modifier = Modifier.fillMaxSize()
            )

        }
        if (pagerState.currentPage == images.size - 1) {
            Button(
                onClick = {
                    go2Main()
                }, colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.primary),
                modifier = Modifier
                    .align(alignment = Alignment.BottomCenter)
                    .padding(32.dp)
            ) {
                Text(stringResource(id = R.string.start_experience), fontSize = 18.sp, color = Color.White)
            }
        }
        HorizontalPagerIndicator(
            pagerState = pagerState,
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(8.dp),
            activeColor = MaterialTheme.colors.primary,
            inactiveColor = Color.White
        )
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    LCZ_Compose_NewsTheme {
        Greeting("Android")
    }
}